package com.ccb.jyzx.expand.ouapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccb.jyzx.expand.ouapi.beans.DepartmentInfo;
import com.ccb.jyzx.expand.ouapi.beans.OrganiztionInfo;
import com.ccb.jyzx.expand.ouapi.beans.sync.DepartmentInfoSync;
import com.ccb.jyzx.expand.ouapi.dao.ouapi.DepartmentDao;
import com.ccb.jyzx.expand.ouapi.dao.ouapi.OrganiztionDao;
import com.ccb.jyzx.expand.ouapi.service.DepartmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ccb.jyzx.expand.ouapi.view.respons.TreeData;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XIEFEI
 * @date 2019-11-12 9:11
 * @Description
 **/
@Service

public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentDao departmentDao;

    @Autowired
    OrganiztionDao organiztionDao;

    @Override
    public DepartmentInfo getById(String id) {
        DepartmentInfo byId = departmentDao.getById(id);
        if (byId!=null&& StringUtils.isNotBlank(byId.getParentDept())){
            DepartmentInfo parent = departmentDao.getById(byId.getParentDept());
            if (parent!=null){
                byId.setParent(parent);
            }
        }

        if (byId!=null&& StringUtils.isNotBlank(byId.getOrgOrganizationId())){
            OrganiztionInfo org = organiztionDao.getById(byId.getOrgOrganizationId());
            if (org!=null){
                byId.setOrganiztionInfo(org);
            }
        }
        return byId;
    }

    @Override
    public List<DepartmentInfo> getAll() {
        List<DepartmentInfo> all = departmentDao.getAll();
        if (all!=null&&!all.isEmpty()){
            List<OrganiztionInfo> orgs = organiztionDao.getAll();
            if (orgs!=null&& !orgs.isEmpty()){//关联组织
                all.forEach(departmentInfo -> {
                    orgs.stream().filter(organiztionInfo -> {
                        return organiztionInfo.getId().equals(departmentInfo.getOrgOrganizationId());
                    }).forEach(organiztionInfo -> {
                        departmentInfo.setOrganiztionInfo(organiztionInfo);
                    });
                });
            }


            //关联父部门
            List<DepartmentInfo> parent= new ArrayList<>();
            parent.addAll(all);
            all.forEach(departmentInfo -> {
                parent.stream().filter(p->{
                    return  p.getId().equals(departmentInfo.getParentDept());
                }).forEach(pp -> {
                    departmentInfo.setParent(pp);
                });
            });
            all.sort((d1,d2)->{
                return d1.getOrderNum().compareTo(d2.getOrderNum());
            });
        }
        return all;
    }

    @Override
    public List<DepartmentInfo> getByOrgId(String orgId) {

        List<DepartmentInfo> deps=  departmentDao.getByOrgId(orgId);
        if (deps!=null &&!deps.isEmpty()){
            List<DepartmentInfo> all = departmentDao.getAll();
            List<OrganiztionInfo> orgs = organiztionDao.getAll();


            deps.forEach(departmentInfo -> {
                if (StringUtils.isNotBlank(departmentInfo.getOrgOrganizationId())){//关联组织信息
                    orgs.stream().filter(organiztionInfo -> {
                        return  organiztionInfo.getId().equals(departmentInfo.getOrgOrganizationId());
                    }).forEach(o -> {
                        departmentInfo.setOrganiztionInfo(o);
                    });
                }
                if (StringUtils.isNotBlank(departmentInfo.getParentDept())){//关联父部门
                    all.stream().filter(pd->{
                        return pd.getId().equals(departmentInfo.getParentDept());
                    }).forEach(p->{
                        departmentInfo.setParent(p);
                    });
                }
            });
        }
        return deps;
    }

    @Override
    public List<DepartmentInfo>  getAllDep() {
        //查询所有组织
        List<OrganiztionInfo> orgs = organiztionDao.getAll();

        //查询所有部门
        List<DepartmentInfo> all = departmentDao.getAll();
        List<DepartmentInfo> result = new ArrayList<>();
        all.stream().filter(departmentInfo -> {
            return StringUtils.isBlank(departmentInfo.getParentDept());
        }).forEach(d->{
            getChildren(d,all,orgs);
            //关联组织
            facthOrg(orgs, d);
            result.add(d);
        });

        return result;
    }

    public List<DepartmentInfo>  getAllDepWithOutOrg() {

        //查询所有部门
        List<DepartmentInfo> all = departmentDao.getAll();
        List<DepartmentInfo> result = new ArrayList<>();
        all.stream().filter(departmentInfo -> {
            return StringUtils.isBlank(departmentInfo.getParentDept());
        }).forEach(d->{
            getChildrenWithOutOrg(d,all);;
            result.add(d);
        });

        return result;
    }

    @Override
    public TreeData getAllDepTree() {
        List<DepartmentInfo> allDep = getAllDepWithOutOrg();
        if (allDep!=null&&!allDep.isEmpty()){
            String js = JSON.toJSONString(allDep);
            String treeStr = js.replaceAll("deptName", "title").replaceAll("id", "key");
            JSONArray jsonArray = JSON.parseArray(treeStr);
            JSONObject jsonObject= new JSONObject();
            jsonObject.put("title","");
            jsonObject.put("key","0");
            jsonObject.put("children",jsonArray);
            TreeData treeData = JSON.parseObject(jsonObject.toJSONString(), TreeData.class);
            return treeData;
        }
        return null;
    }

    @Override
    public List<DepartmentInfo> getByOrgIdDep(String orgId) {
        List<DepartmentInfo> byOrgId = getByOrgId(orgId);
        if (byOrgId!=null&&!byOrgId.isEmpty()){
            //查询所有部门
            List<DepartmentInfo> all = departmentDao.getAll();
            List<DepartmentInfo> result = new ArrayList<>();
            byOrgId.forEach(d->{
                getChildrenWithOutOrg(d,all);;
                result.add(d);
            });

            return result;
        }
        return null;
    }

    @Override
    public List<DepartmentInfoSync> getAllSync() {
        return departmentDao.getAllSync();
    }

    @Override
    public void insert(DepartmentInfoSync departmentInfoSync) {
        departmentDao.save(departmentInfoSync);
    }

    @Override
    public void update(DepartmentInfoSync departmentInfoSync) {
        departmentDao.update(departmentInfoSync);
    }

    @Override
    public void deleteById(String id) {
        departmentDao.deleteById(id);
    }

    //关联组织
    private void facthOrg(List<OrganiztionInfo> orgs, DepartmentInfo d) {
            if(orgs!=null&&!orgs.isEmpty()&& StringUtils.isNotBlank(d.getOrgOrganizationId())){
                orgs.stream().filter(o->{
                    return o.getId().equals(d.getOrgOrganizationId());
                }).forEach(o->{
                    d.setOrganiztionInfo(o);
                });
            }
    }

    private void getChildren(DepartmentInfo departmentInfo,List<DepartmentInfo> all,List<OrganiztionInfo> orgs){
        ArrayList<DepartmentInfo> childs= new ArrayList<DepartmentInfo>();
        all.stream().filter(d -> {
            return d.getParentDept()!=null&& d.getParentDept().equals(departmentInfo.getId());
        }).forEach(c->{
            getChildren(c,all,orgs);
            //关联组织
            facthOrg(orgs,c);
            childs.add(c);
        });
        childs.sort((a,b)->{
            return  a.getOrderNum().compareTo(b.getOrderNum());
        });
        departmentInfo.setChildren(childs);
    }

    private void getChildrenWithOutOrg(DepartmentInfo departmentInfo,List<DepartmentInfo> all){
        ArrayList<DepartmentInfo> childs= new ArrayList<DepartmentInfo>();
        all.stream().filter(d -> {
            return d.getParentDept()!=null&& d.getParentDept().equals(departmentInfo.getId());
        }).forEach(c->{
            getChildrenWithOutOrg(c,all);

            childs.add(c);
        });
        childs.sort((a,b)->{
            return  a.getOrderNum().compareTo(b.getOrderNum());
        });
        departmentInfo.setChildren(childs);
    }




}
